#!/bin/bash
set -e

KEXT="/Library/Extensions/softu2f.kext"
LAUNCH_AGENTS_DIR="$HOME/Library/LaunchAgents"
LAUNCH_AGENT_PLIST="$LAUNCH_AGENTS_DIR/com.github.SoftU2F.plist"

# Make sure the kext is loaded
kextutil $KEXT

# This directory should already exist, but some users have had issues with it
# being missing.
mkdir -p $LAUNCH_AGENTS_DIR

# Write a LaunchAgent plist so app starts at login
cat > $LAUNCH_AGENT_PLIST << EOT
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
	<dict>
		<key>Label</key>
		<string>com.github.SoftU2F</string>
		<key>Program</key>
		<string>/Applications/SoftU2F.app/Contents/MacOS/SoftU2F</string>
		<key>ProgramArguments</key>
		<array>
			<string>/Applications/SoftU2F.app/Contents/MacOS/SoftU2F</string>
		</array>
		<key>RunAtLoad</key>
		<true/>
		<key>KeepAlive</key>
		<true/>
	</dict>
</plist>
EOT

# Make sure the plist is owned by the user (otherwise it runs as root)
chown "${USER}:staff" $LAUNCH_AGENT_PLIST

# Launch the app
sudo -u "${USER}" launchctl load $LAUNCH_AGENT_PLIST

exit 0
